package com.xfzp.android.ms;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

public class Main extends Activity {
	public static final String DATA_NAME = "com.xfzp.android.ms.DATA";
	private long playTimes = 0;
	private int rotateSum = 0;

	private int conViewId;
	private KubeSprite sprite;

	private View menuView;
	private View gameView;
	private ViewGroup glGroup;
	private TextViewTexter leftText;
	private TextViewTexter rightText;

	private Timer timer;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置隐藏标题
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		LayoutInflater inflater = getLayoutInflater();
		menuView = inflater.inflate(R.layout.menu, null);

		setContentView(menuView);
		conViewId = R.layout.menu;

		gameView = inflater.inflate(R.layout.game, null);
		glGroup = (ViewGroup) gameView.findViewById(R.id.glGroup);
		leftText = new TextViewTexter(
				(TextView) gameView.findViewById(R.id.leftText));
		rightText = new TextViewTexter(
				(TextView) gameView.findViewById(R.id.rightText));

		SharedPreferences data = getSharedPreferences(DATA_NAME, MODE_PRIVATE);
		// 设置初始旋转次数和时间
		addRotateSum(data.getInt("rotateSum", 0));
		stepPlayTimes(data.getLong("playTimes", 0));

		sprite = new KubeSprite(this, data.getString("cubeOrder", null),
				null/*data.getString("modelView", null)*/);
		timer = new Timer();
	}

	@Override
	protected void onPause() {
		super.onPause();
		pauseGame();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (R.layout.game == conViewId) {
				pauseGame();
			} else {
				exitGame();
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 按钮事件
	 * 
	 * @param view
	 */
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.startButton:
			playGame();
			break;
		case R.id.exitButton:
			exitGame();
			break;
		}
	}

	/**
	 * 增加旋转次数
	 * 
	 * @param i
	 */
	public void addRotateSum(int i) {
		rotateSum += i;
		rightText.setText("旋转次数：" + rotateSum + "次");
		runOnUiThread(rightText);
	}

	/**
	 * 增加旋转时长
	 */
	private void stepPlayTimes(long ms) {
		playTimes += ms;
		leftText.setText("总时长：" + (playTimes / 1000) + "秒");
		runOnUiThread(leftText);
	}

	/**
	 * 开始游戏
	 */
	private void playGame() {
		glGroup.removeAllViews();
		glGroup.addView(new GLView(getApplication(), sprite));

		timer.start();
		new Thread(timer).start();

		setContentView(gameView);
		conViewId = R.layout.game;
	}

	/**
	 * 暂停游戏
	 */
	private void pauseGame() {
		timer.pause();
		setContentView(menuView);
		conViewId = R.layout.menu;
	}

	/**
	 * 退出游戏
	 */
	private void exitGame() {
		SharedPreferences data = getSharedPreferences(DATA_NAME, MODE_PRIVATE);
		Editor editor = data.edit();
		editor.putLong("playTimes", playTimes);
		editor.putInt("rotateSum", rotateSum);
		StringBuffer cos = new StringBuffer();
		int[] coi = sprite.getCubeOrder();
		for (int c : coi) {
			cos.append(",").append(c);
		}
		editor.putString("cubeOrder", cos.substring(1));
		
		StringBuffer mvs = new StringBuffer();
		float[] mv = sprite.getModelView();
		for (float m : mv) {
			mvs.append(",").append(m);
		}
		editor.putString("modelView", mvs.substring(1));
		editor.commit();

		this.finish();
	}

	/**
	 * 计时器线程
	 * 
	 * @author AndroidApe
	 * @version 2011-12-9
	 */
	class Timer implements Runnable {
		private boolean run = true;

		@Override
		public void run() {
			long t1 = System.currentTimeMillis();
			long t2;
			while (run) {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e1) {
				}
				t2 = System.currentTimeMillis();
				stepPlayTimes(t2 - t1);
				t1 = t2;
			}
		}

		public void start() {
			run = true;
		}

		public void pause() {
			run = false;
		}
	}
}